import java.util.*;

/**
 * Created by 蜡笔小新丶 on 2017/5/12.
 */

public class Practice {
    protected List<String> list;
    protected List<String> list1;
    protected List<String> list2,list4;
    private List<Character>list3;
    Random ran = new Random();
    Original ori = new Original();
    Counts count = new Counts();
    private String sym;
    private char[] arr;

    public Practice() {
        list = new ArrayList<String>();
        list1 = new ArrayList<String>();
        list2 = new ArrayList<String>();
        list3 = new ArrayList<Character>();
        list4 = new ArrayList<String>();
    }

    public void Ti(int number, int many) {
        many = Ticlass(many);
        for (int j = 0; j < number; j++) {
            String ti = "";
            String Ti = "";
            for (int i = 0; i < many; i++) {
                int A = ran.nextInt(8) + 1;
                int B = ran.nextInt(5);
                int C = ran.nextInt(5);
                if (parity(i)) {
                    if (list1.indexOf("( ") == -1)
                        list1.add(getSym() + " ");
                    else if (list1.size() - list1.lastIndexOf("( ") > 4) {
                        if (list1.lastIndexOf(") ") - list1.lastIndexOf("( ") < 0 && B == 0) {
                            list1.add(") ");
                            list1.add(getSym() + " ");
                        } else list1.add(getSym() + " ");
                    } else list1.add(getSym() + " ");
                } else if (i == many - 1) {
                    if (list1.lastIndexOf("( ") - list1.lastIndexOf(") ") > 0) {
                        list1.add(A + " ");
                        list1.add(") ");
                    } else
                        list1.add(A + " ");
                } else if (i == 0) {
                    list1.add(A + " ");
                } else if (list1.lastIndexOf(") ") != -1) {
                    if (list1.lastIndexOf(") ") - list1.lastIndexOf("( ") > 0 && B == 0) {
                        list1.add("( ");
                        list1.add(A + " ");
                    } else
                        list1.add(A + " ");
                } else if (list1.indexOf("( ") == -1 && B == 0) {
                    list1.add("( ");
                    list1.add(A + " ");
                } else
                    list1.add(A + " ");
            }
            for (String i : list1)
                ti += i;
            StringTokenizer tokenizer = new StringTokenizer(ti);
            while (tokenizer.hasMoreTokens()) {
                char N = tokenizer.nextToken().charAt(0);
                list3.add(N);
            }
            list1.clear();
            Collections.sort(list3);
            for(char i:list3)
                Ti += i;
            list3.clear();
            ori.evaluate(ti);
            String restrult = count.evaluate(ori.getMessage());
            if (list2.indexOf(restrult) >= 0)
                if (list4.indexOf(Ti)>=0)
                    j--;
                else if(Ti.indexOf("(")>=0)
                    j--;
                else {
                    list4.add(Ti);
                    list.add(ti);
                }
            else {
                list2.add(restrult);
                list4.add(Ti);
                list.add(ti);
            }
        }
    }

public String getSym(){
        int A=ran.nextInt(4);
        switch(A){
        case 0:
        sym="+";
        break;
        case 1:
        sym="-";
        break;
        case 2:
        sym="*";
        break;
        case 3:
        sym="/";
        break;
        }
        return sym;
        }

public boolean parity(int num){
        if(num%2==1)
        return true;
        else
        return false;
        }

public int Ticlass(int many){
        int A=1;
        if(many>0)
        for(int i=0;i<many; i++)
        A+=2;
        return A;
        }
        }